//
//  CyclingRequest.m
//  Cycling
//
//  Created by CSS on 4/7/23.
//

#import "CyclingRequest.h"
#import "BaseBodyModel.h"

@implementation CyclingRequest

+ (void)postCyclingDataWithApi:(NSString *)api params:(NSDictionary *)params complete:(void(^)(BOOL success, id response, NSString *reason))complete
{
    NSString *requestApi = [NSString stringWithFormat:@"%@%@", BaseUrl, api];
    [self basePOST:requestApi cache:NO autoHideLoading:NO parms:params result:^(id resultObject, NSError *error) {
        if(resultObject == nil) {
            NSError *underlineError = [error.userInfo valueForKey:NSUnderlyingErrorKey];
            NSData *errorData = [underlineError.userInfo valueForKey:@"com.alamofire.serialization.response.error.data"];
            NSString *errorString = [[NSString alloc] initWithData:errorData encoding:NSUTF8StringEncoding];
            DebugLog(@"错误:%@", errorString);
            if(complete != nil) {
                if(errorString.length > 5) {
                    complete(NO, nil, errorString);
                } else if(error) {
                    complete(NO, nil, [NSString stringWithFormat:@"%@", error]);
                } else {
                    complete(NO, nil, @"未返回数据");
                }
            }
            return;
        }
        BaseBodyModel *baseModel = [BaseBodyModel yy_modelWithJSON:resultObject];
        if(baseModel.code == 0) {
            if(complete != nil) {
                complete(NO, nil, baseModel.msg.length == 0 ? @"未返回数据" : baseModel.msg);
            }
            return;
        }
        if(complete != nil) {
            complete(YES, baseModel.data, nil);
        }
    }];
}

+ (void)getAddressInfoWithParams:(NSDictionary *)params complete:(void(^)(NSString *suggessAddress))complete
{
    NSString *accessToken = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"MBXAccessToken"];
    if(accessToken == nil) {
        if(complete != nil) {
            complete(nil);
        }
        return;
    }
    NSString *searchURL = [NSString stringWithFormat:@"https://api.mapbox.com/geocoding/v5/mapbox.places/%@,%@.json?limit=1&access_token=%@", [params valueForKey:@"lon"], [params valueForKey:@"lat"], accessToken];
    [self baseGET:searchURL cache:NO autoHideLoading:NO parms:nil request:^(id  _Nullable resultObject, NSError * _Nullable error) {
        NSString *address = [[[[resultObject valueForKey:@"features"] firstObject] valueForKey:@"properties"] valueForKey:@"address"];
        if(complete) {
            complete(address);
        }
    }];
    
}

/**
 上传图片
 */
+ (void)uploadImageWithFileData:(NSData *)file complete:(void(^)(NSString *imagePath))complete
{
    NSString *requestApi = [NSString stringWithFormat:@"%@%@", BaseUrl, Cyc_API_UploadImage];
    [self basePOST:requestApi cache:NO autoHideLoading:NO parms:@{} data:file result:^(id  _Nullable resultObject, NSError * _Nullable error) {
        if(resultObject == nil) {
            NSData *errorData = [error.userInfo valueForKey:@"com.alamofire.serialization.response.error.data"];
            NSString *errorString = [[NSString alloc] initWithData:errorData encoding:NSUTF8StringEncoding];
            DebugLog(@"错误:%@", errorString);
            if(complete) {
                complete(nil);
            }
            return;
        }
        BaseBodyModel *baseModel = [BaseBodyModel yy_modelWithJSON:resultObject];
        NSString *url = [baseModel.data valueForKey:@"url"];
        complete(url);
        if(complete) {
            complete(nil);
        }
    }];
}

@end
